package com.android;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class HighScore extends Activity implements OnClickListener {
	Database database = new Database(this);
	SQLiteDatabase db;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.highscore);
        
        //add a button
        Button button4 = (Button) findViewById(R.id.back);
        button4.setOnClickListener(this);
        //add another button
        Button button5 = (Button) findViewById(R.id.reset);
        button5.setOnClickListener(this);
        
        //start database and make it readable
		db = database.getReadableDatabase();
		//load the namefield
		TextView td=(TextView)findViewById(R.id.name);
		
		//open the table
		String sql = "Select * from "+ Database.TABLEname+";";
		//create a cursor for the table
        Cursor cursor = db.rawQuery(sql, new String [] {});
        //move the cursor to the first slot
        cursor.moveToFirst();
        //3 variables for the different columns, name, time, turns
        int a = cursor.getColumnIndex(Database.ColName);
        int b = cursor.getColumnIndex(Database.ColTime);
        int c = cursor.getColumnIndex(Database.ColTurns);
        //looping through the table, and using the 3 column variables for each row.
        for(int i = 0; i< cursor.getCount(); i++){
        	String s1 = cursor.getString(a);
        	String s2 = cursor.getString(b);
        	String s3 = cursor.getString(c);
        	if(i == 0)
        		//first element will not have the previous text
        		td.setText("Player: " +s1 +" Time: "+ s2 + " Turns: " + s3 + "\n");
        	else
        		//all other elements get the old text from the name list.
        		td.setText(td.getText() + "Player: " +s1 +" Time: "+ s2 + " Turns: " + s3 + "\n");
        	//move to the next element	
        	cursor.moveToNext();
        }
        //close the cursor and database
        cursor.close();
        db.close();
    }
	


	@Override
	public void onClick(View v) {
		if(v == findViewById(R.id.back))
		{
			//go back to the main menu
			this.finish();
    		Intent intent  = new Intent(getBaseContext(), MemoryActivity.class);
    		startActivity(intent);
		}else if(v == findViewById(R.id.reset)){
			//delete the database. the name of the database is a constant in class Database.
			// prepare the alert box
            AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
 
            // set the text message
            alertbox.setMessage("Do you want to clear the database?");
 
            // add a neutral button to the alert box and assign a click listener
            alertbox.setCancelable(false)
            //if the player presses yes do this
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                	//delete the database
        			database.deleteDB();
        			//Close the current activity and start it again. A way to refresh the screen
        			HighScore.this.finish();
            		Intent intent  = new Intent(getBaseContext(), HighScore.class);
            		startActivity(intent);
                }
            })
            //if the player presses no do this
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                	//close the dialog box
                     dialog.cancel();
                }
            });
            // show it
            alertbox.show();

		}
		
	}
}
